//判断是不是ie
var ac=navigator.userAgent;
if (navigator.userAgent.indexOf("MSIE") > -1) {
    var ss = navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE") + 4, navigator.userAgent.indexOf("MSIE") + 9);
    if (parseInt(ss) < 10) {
       $('.banben').css({'display':'block'});
    }
}
$(function(){
	//版本
	$('.banben-close').on('click',function(){
		$('.banben').css({'display':'none'});
	});
	var url = common.url();
    var keycode = 0;
	// 搜索
	$('.htop-search .search-btn').bind('click',function(){
		if($('.seach').val()!=''){
			window.location.href = '/search.htm?keyword='+ encodeURI($('.seach').val());
		}
	})
	// 判断是否登陆
	var loginurl= common.Interface('onlogin')[0]
	$.ajax({
		url : loginurl,
		type: "POST",
		error: function(request) {
			//console.log(request)
		},
		dataType:"json",
		success: function(data) {
			if(data.ret){
				// 登陆成功
				if(!data.data){
					$('.login-before').css({'display':'block'});
					$('.login-after').remove();
				}else{
					$('.login-after').css({'display':'block'});
					$('.login-before').remove();
				}
			}
		}
	})
	//关于登陆框的内容提示
	$('.inp-box input').on('input',function(){
		$(this).parents('.inp-box').children('.input-val').css({'display':'none'});
	})
	$('.inp-box input').blur(function(){
		if($(this).val()==''){
			$(this).parents('.inp-box').children('.input-val').css({'display':'block'});
		}
	})
	$('.login-show').bind('click',function(){
		$('.header-box').css({'display':'block'});
		$('.boxbg').css({'display':'block'});
		$('.tanbox').css({'display':'none'});
		$('.login-box').css({'display':'block'});
		$('.showboxtitle').html('登陆');
		keycode = 1;
		
	})
	$('.register-show').bind('click',function(){
		$('.header-box').css({'display':'block'});
		$('.boxbg').css({'display':'block'});
		$('.tanbox').css({'display':'none'});
		$('.register-box').css({'display':'block'});
		$('.showboxtitle').html('注册');
		keycode = 2;
	})
	$('.lostpasbtn').bind('click',function(){
		$('.header-box').css({'display':'block'});
		$('.boxbg').css({'display':'block'});
		$('.tanbox').css({'display':'none'});
		$('.repas-box').css({'display':'block'});
		$('.showboxtitle').html('忘记密码');
		keycode = 3;
	})
	$('.respas').bind('click',function(){
//		$('.header-box').css({'display':'block'});
//		$('.boxbg').css({'display':'block'});
//		$('.tanbox').css({'display':'none'});
//		$('.change-box').css({'display':'block'});
//		$('.showboxtitle').html('修改密码');
		window.location.href="/user.htm"
	})
	
	$('.closeboxbtn').bind('click',function(){
		$('.header-box').css({'display':'none'});
		$('.boxbg').css({'display':'none'});
		keycode = 0;
	});
	// 注销
	$('.lostlogin').bind('click',function(){
		$.ajax({
				url : '/api/logout.json',
				data:{'userName':$('.sur1').eq(0).val(),
					'password':$('.sur1').eq(1).val()
				},
				type: "POST",
				error: function(request) {
					console.log(request)
				},
				dataType:"json",
				success: function(data) {
					if(data.ret){
						if(data.data){
							window.location.reload();
						}
					}
				}
		})
	});
	//登陆验证
	$('.login-box .allbtn').bind('click',function(){
		var onf1 = true;
		$('.sur1').each(function(i,e){
			if(e.value == ''){
				e.focus();
				$('.sur1').eq(i).css({'border-bottom':'2px solid #b99663'});
				$('.baocuo').html('输入框内容不能为空');
				onf1 = false;
				return false
			}
		})
		if(onf1){
			var jk = common.Interface('login')[0];
			$.ajax({
					url : jk,
					data:{'userName':$('.sur1').eq(0).val(),
						'password':$('.sur1').eq(1).val()
					},
					type: "POST",
					error: function(request) {
						console.log(request)
					},
					dataType:"json",
					success: function(data) {
						if(data.ret){
							$('.respas').attr({'userid':data.data.id});
							window.location.reload();
						}else{
							$('.baocuo').html(data.errmsg);
							if(data.errmsg == "输入密码次数超过限制，请稍后再试"){
                                $('.login-box .allbtn').attr('disabled','disabled');
                                $(document)[0].cookie = 'ckey='+30;
                                console.log('cookie 设置完毕');
                                t = setInterval(function(){
                                    setCookie();
                                },1000)
							}
						}
					}
			})
		}
		
	});
    //获取cookies
    function  getCookie() {
        var cookie = $(document)[0].cookie;
        var citem = cookie.split(',');
        var ckey = citem.filter(function (item) {
            return item.split('=')[0].trim()=='ckey';
        })
        // 获得 时间  cval
		if(ckey.length !== 0){
            cval = ckey[0].split('=')[1];
		}else{
            cval = 0;
		}
        return cval;
    }
    //查看cookie
    function setStyle() {
        var cval = getCookie();
        if(cval>1){
            $('.login-box .allbtn').text(cval);
            $('.login-box .allbtn').attr('disabled',true);
            $('.login-box .allbtn').css('cursor','not-allowed');
            t = setInterval(function(){
                setCookie();
            },1000)
        }else{
            $('.login-box .allbtn').text('登录');
            $('.login-box .allbtn').attr('disabled',false);
            $('.login-box .allbtn').css('cursor','pointer');
        }
    }
    setStyle();
    //设置cookie
    function setCookie() {
        var cval = getCookie();
        if(cval>1){
            $(document)[0].cookie = 'ckey='+(cval-1);
            $('.login-box .allbtn').text(cval);
            $('.login-box .allbtn').attr('disabled','disabled');
        }else{
            $('.login-box .allbtn').text('登录');
            $('.login-box .allbtn').attr('disabled',false);
            $(document)[0].cookie = 'ckey='+0;
        }
    }
	//注册验证
	$('.register-box .allbtn').bind('click',function(){
		var onf2 = true;
		$('.sur2').each(function(i,e){
			if(e.value == ''){
				e.focus();
				$('.sur2').eq(i).css({'border-bottom':'2px solid #b99663'});
				$('.baocuo').html('输入框内容不能为空');
				onf2 = false;
				return false
			}
		})
		if($('.pas01').val()!=$('.pas02').val()){
			$('.pas01').focus();
			$('.pas01').css({'border-bottom':'2px solid #b99663'});
			$('.baocuo').html('两次输入密码不同');
			onf2 = false;
			return false
		}
		if(onf2){
			var jk = common.Interface('register')[0];
			$.ajax({
					url : jk,
					data:{'userName':$('.sur2').eq(0).val(),
						'password':$('.sur2').eq(3).val(),
						'phone':$('.sur2').eq(1).val(),
						'email':$('.sur2').eq(2).val()
					},
					type: "POST",
					error: function(request) {
						console.log(request)
					},
					dataType:"json",
					success: function(data) {
						if(data.ret){
							$('.header-box').css({'display':'block'});
							$('.boxbg').css({'display':'block'});
							$('.tanbox').css({'display':'none'});
							$('.login-box').css({'display':'block'});
							$('.showboxtitle').html('登陆');
							keycode = 1;
						}else{
							$('.baocuo').html(data.errmsg);
						}
					}
			})
		}
	})
	
	//忘记密码验证
	$('.repas-box .allbtn').bind('click',function(){
		var onf3 = true;
		$('.sur3').each(function(i,e){
			if(e.value == ''){
				e.focus();
				$('.sur3').eq(i).css({'border-bottom':'2px solid #b99663'});
				$('.baocuo').html('输入框内容不能为空');
				onf3 = false;
				return false
			}
		})
		if($('.pas11').val()!=$('.pas12').val()){
			
			$('.pas11').focus();
			$('.pas11').css({'border-bottom':'2px solid #b99663'});
			$('.baocuo').html('两次输入密码不同');
			onf3 = false;
			return false
		}
		if(onf3){
			var jk = common.Interface('lostpas')[0];
			$.ajax({
					url : jk,
					data:{
						'password':$('.sur3').eq(2).val(),
						'phone':$('.sur3').eq(0).val(),
						'email':$('.sur3').eq(1).val()
					},
					type: "POST",
					error: function(request) {
						console.log(request)
					},
					dataType:"json",
					success: function(data) {
						if(data.ret){
							$('.baocuo').html('密码修改成功,请登陆');
							setTimeout(function(){
								$('.header-box').css({'display':'block'});
								$('.boxbg').css({'display':'block'});
								$('.tanbox').css({'display':'none'});
								$('.login-box').css({'display':'block'});
								$('.showboxtitle').html('登陆');
							},200)
							
						}else{
							$('.baocuo').html(data.errmsg);
						}
					}
			})
		}
	})
	
	//修改密码验证
	$('.change-box .allbtn').bind('click',function(){
		var onf4 = true;
		$('.sur4').each(function(i,e){
			if(e.value == ''){
				e.focus();
				$('.sur4').eq(i).css({'border-bottom':'2px solid #b99663'});
				$('.baocuo').html('输入框内容不能为空');
				onf4 = false;
				return false
			}
		})
		if($('.pas21').val()!=$('.pas22').val()){
			$('.pas21').focus();
			$('.pas21').css({'border-bottom':'2px solid #b99663'});
			$('.baocuo').html('两次输入密码不同');
			onf4 = false;
			return false
		}
		if(onf4){
			var jk = common.Interface('changepas')[0];
			$.ajax({
					url : jk,
					data:{
						'oldPassword':$('.sur4').eq(0).val(),
						'newPassword':$('.sur4').eq(1).val()
					},
					type: "POST",
					error: function(request) {
						console.log(request)
					},
					dataType:"json",
					success: function(data) {
						if(data.ret){
							$('.baocuo').html('密码修改成功,请登陆');
							window.location.reload();
						}else{
							$('.baocuo').html(data.errmsg);
						}
					}
			})
		}
	})
	
	$('.inp-box input').on('input',function(){
		$(this).css({'border-bottom':'2px solid #cdcdcd'})
		$('.baocuo').html('')
	})
	// 关于按键的操作
	document.onkeydown=function(event){
	  var e = event || window.event || arguments.callee.caller.arguments[0];
	  if(e && e.keyCode==13){ // 按 Esc 
		  if(keycode == 1){
			  $('.login-box .allbtn').trigger('click')
		  }else  if(keycode == 2){
			  $('.register-box .allbtn').trigger('click')
		  }else  if(keycode == 3){
			  $('.repas-box .allbtn').trigger('click')
		  }
		  
	   }
	}; 
	
	//关于下拉菜单
	var timer11=null;
	var timer22=null;
	var o=0;
	$('.nav-box li').hover(function(){
		$('.nav-showli .nav-lis').css({'top':40,'opacity':0,'display':'none'});
		var This = $('.nav-showli .nav-lis').eq($(this).index());
		if($(this).index() == 0){
			This.css({'left':6});
		}else if($(this).index() == 1){
			This.css({'left':150});
		}else{
			This.css({'left':60+120*($(this).index())});
		}
		clearInterval(timer22);
		clearTimeout(timer11);
		This.stop(true,true);
		timer22=setInterval(function(){
			o++;
			if(o>4){
				This.css({'display':'block'}).animate({'top':0,'opacity':1},180,'swing');
				This.animate({'top':10,'opacity':1},100,'swing');
				o=0;
				clearInterval(timer22);
			}
		},40)
		
	},function(){
		var This = $('.nav-showli .nav-lis').eq($(this).index());
		clearTimeout(timer11);
		clearInterval(timer22);
		$('.nav-showli .nav-lis').stop(true,true);
		timer11=setTimeout(function(){
			This.animate({'top':0},100,'linear');
			This.animate({'top':40,'opacity':0},180,'swing',function(){
				This.css({'display':'none'});
			});
		},400)
		This.hover(function(){
			clearInterval(timer22);
			clearTimeout(timer11);
		},function(){
			This.animate({'top':0},100,'linear');
			This.animate({'top':40,'opacity':0},180,'swing',function(){
				This.css({'display':'none'});
			});
		})
		
	})
	
})
